package com.libinyn.base.config.db;


import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;


/**
 * 数据源配置
 * @fileName: CrsDataSource.java
 * @author: bin.li01
 * @date: 2017/10/26 20:15
 * @version: v1.0.0
 */
@ComponentScan
@Configuration
@ConfigurationProperties(prefix="spring.datasource")
public class CrsDataSource {



    private String username;

    private String password;

    private String driverClassName;

    private String url;

    @Value("${spring.datasource.hikari.max-lifetime}")
    private String maxLifetime;

    @Value("${spring.datasource.hikari.maximum-pool-size}")
    private String maximumPoolSize;

    private HikariDataSource dataSource = new HikariDataSource();

    @Resource(name = "defaultEncryptor")
    private DefaultEncryptor defaultEncryptor;

    @Bean
    public DataSource getDataSource() {

        dataSource.setUsername(username);
        dataSource.setJdbcUrl(url);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setMaxLifetime(Integer.valueOf(maxLifetime));
        dataSource.setMaximumPoolSize(Integer.valueOf(maximumPoolSize));

        dataSource.setPassword(defaultEncryptor.encrypt(password));
        return dataSource;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }


}